package com.cloudfast.utils;

import java.io.File;

public class OsUtil {
    private static String OS = System.getProperty("os.name").toLowerCase();

    private static OsUtil _instance = new OsUtil();

    private OsType platform;

    private OsUtil() {
    }

    public static boolean isLinux() {
        return OS.indexOf("linux") >= 0;
    }

    public static boolean isMacOS() {
        return OS.indexOf("mac") >= 0 && OS.indexOf("os") > 0 && OS.indexOf("x") < 0;
    }

    public static boolean isMacOSX() {
        return OS.indexOf("mac") >= 0 && OS.indexOf("os") > 0 && OS.indexOf("x") > 0;
    }

    public static boolean isWindows() {
        return OS.indexOf("windows") >= 0;
    }

    /**
     * 获取操作系统名字
     *
     * @return 操作系统名
     */
    public static OsType getOSname() {
        if (isLinux()) {
            _instance.platform = OsType.Linux;
        } else if (isMacOS()) {
            _instance.platform = OsType.Mac_OS;
        } else if (isMacOSX()) {
            _instance.platform = OsType.Mac_OS_X;
        } else if (isWindows()) {
            _instance.platform = OsType.Windows;
        }
        return _instance.platform;
    }

    public static String getLastWindowsDisk() {
        File[] roots = File.listRoots();
        String diskName = roots[roots.length - 1].toString();
        return diskName.substring(0, diskName.length() - 1);
    }

    enum OsType {

        Linux("Linux"), Mac_OS("Mac OS"), Mac_OS_X("Mac OS X"), Windows("Windows");

        private OsType(String desc) {
            this.description = desc;
        }

        public String toString() {
            return description;
        }

        private String description;
    }
}
